<template>
  <div class="form">
    <el-form label-position="top" label-width="80px" :model="menuForm">
      <el-form-item label="账号">
        <el-input v-model="menuForm.account"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="menuForm.password"></el-input>
      </el-form-item>
      <el-form-item label="类型">
        <el-input v-model="menuForm.type"></el-input>
      </el-form-item>
      <el-form-item label="关联用户">
        <el-input v-model="userIdShowName" disabled></el-input>
        <el-button type="primary" @click="onSelectUser" size="small">选择</el-button>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="menuForm.description"></el-input>
      </el-form-item>
      <el-form-item label="是否禁用">
        <el-switch
          v-model="menuForm.disabled"
          active-value=1
          inactive-value=0>
        </el-switch>
      </el-form-item>
      <div style="text-align: right;">
        <el-form-item>
          <el-button type="primary" @click="onAdd">保存</el-button>
          <el-button @click="onClose">取消</el-button>
        </el-form-item>
      </div>
    </el-form>
  <el-dialog
    title="选择用户"
    :visible.sync="dialogVisible"
    width="40%" :modal="false">
    <span>
      <v-selectUser :randomKey.sync="randomKey" :checkedUser.sync="checkedUser"></v-selectUser>
    </span>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="doSelectUser">确 定</el-button>
    </span>
  </el-dialog>
  </div>
</template>

<script>
import api from '@/api'
import selectUser from '@/views/public/selectUser.vue'

export default {
  name: 'account-add',
  components: {
    'v-selectUser': selectUser
  },
  props: {
  },
  data () {
    return {
      menuForm: {
      },
      userIdShowName: '',
      checkedUser: [],
      randomKey: 0,
      dialogVisible: false
    }
  },
  mounted () {
  },
  methods: {
    onAdd () {
      var param = this.menuForm
      param.parentId = this.parentId
      api.sysAccountInsert(param).then(res => {
        this.$message({ type: 'info', message: '已添加' })
        this.onClose()
        this.$emit('refresh')
      })
    },
    onSelectUser () {
      this.dialogVisible = true
    },
    doSelectUser () {
      // console.log(this.checkedUser)
      if (this.checkedUser.length > 0) {
        this.menuForm.userId = this.checkedUser[0].id
        this.userIdShowName = this.checkedUser[0].nameZh
      } else {
        this.menuForm.userId = ''
        this.userIdShowName = ''
      }
      this.dialogVisible = false
    },
    onClose () {
      this.$emit('closeDrawer')
    }
  }
}
</script>

<style lang="scss">
  .form {
    padding: 10px;
  }
</style>
